package com.jushu.storbox.view.mdmview;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.net.Uri;
import android.widget.Toast;
import android.widget.VideoView;

import com.jushu.storbox.R;
import com.jushu.storbox.util.LogUtil;
import com.jushu.storbox.util.PublicTools;
import com.jushu.storbox.view.viewImpl.VideoController;

public class VideoDao extends VideoView implements IVideoDao, IBaseDao {

	private final static String TAG = "VideoDao";
	//4=r 2=w 1=x
	private final static String DEFAULT_PERMISSION = "711";//
	private final static String READ_PERMISSION = "744";//
	
	private Context context;
	private Activity activity;
	private Uri uri;
	private boolean videoError = false;
	
	public VideoDao(Context context, Uri uri) {
		super(context);
		// TODO Auto-generated constructor stub
		this.context = context;
		this.activity = (Activity) context;
		this.uri = uri;
		
		initData();
	}

	private void initData() {
		// TODO Auto-generated method stub
		activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		if(activity.getRequestedOrientation()== ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
			activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		}	
        //
		PublicTools.setFilePermission(uri.getPath(), READ_PERMISSION);
		//
        if(uri!= null){   		        
//        	setVideoURI(uri);
        	setVideoPath(uri.getPath());
        	setTag(uri);
        }
        VideoController mMediaController = new VideoController(context, this);
		setMediaController(mMediaController);
		setOnErrorListener(listen_video_err);
		setOnCompletionListener(listen_video_complete);

		start();
		
		IntentFilter intentFilter = new IntentFilter();
		intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
		intentFilter.addAction(Intent.ACTION_SCREEN_ON);
		context.registerReceiver(mReceiver_Screen_off_on, intentFilter);
	}
	
	@Override
	public void onDestory() {
		LogUtil.i(TAG, "unregisterReceiver Broadcast mReceiver_Screen_off_on");
		PublicTools.setFilePermission(uri.getPath(), DEFAULT_PERMISSION);
		if (mReceiver_Screen_off_on != null) {
			context.unregisterReceiver(mReceiver_Screen_off_on);
			mReceiver_Screen_off_on = null;
		}
	}
	
	private BroadcastReceiver mReceiver_Screen_off_on = new BroadcastReceiver() {
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();

			if (action.equals(Intent.ACTION_SCREEN_OFF)) {
				pause();
				
			}else if(action.equals(Intent.ACTION_SCREEN_ON)){
				start();
				
			}
		}
	};
	
	private OnErrorListener listen_video_err = new OnErrorListener() {

		@Override
		public boolean onError(MediaPlayer mp, int what, int extra) {
			videoError = true ;
			Toast.makeText(context, context.getResources().getString(R.string.play_error),Toast.LENGTH_LONG).show();
//			listen_video_complete.onCompletion(mp);
			PublicTools.setFilePermission(uri.getPath(), DEFAULT_PERMISSION);
			activity.finish();
			return true;
		}
	};
	
	private OnCompletionListener listen_video_complete = new OnCompletionListener() {
		@Override
		public void onCompletion(MediaPlayer arg0) {
			// TODO Auto-generated method stub
//			activity.finish();
			pause();
			return;
		}
	};
	
	public boolean getVideoErrorState(){
		return videoError;
	}

	@Override
	public void onStop() {
		// TODO Auto-generated method stub
		pause();
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
//		start();
		pause();
	}
}
